#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_NUMBER 100


void initialize_number_generator(void);
int get_new_secret_number(void);
void read_guesses();

int main(void)
{
    char command;

    printf("Guess the secret number between 1 and %d.\n\n", MAX_NUMBER);
    initialize_number_generator();

    do
    {
        printf("A new number has been chosen.\n");
        read_guesses();
        printf("Play again?(Y/N) ");
        scanf(" %c", &command);
        printf("\n");
    } while (command == 'Y' || command == 'y');

    return 0;
}

void initialize_number_generator(void)
{
    srand((unsigned)time(NULL));
}

int get_new_secret_number(void)
{
    return rand() % MAX_NUMBER + 1;
}

void read_guesses()
{
    int guess, num_guesses = 0,
        secret_number = get_new_secret_number();

    for (;;)
    {
        num_guesses++;  //count the times of guess
        printf("Enter guess: ");
        scanf("%d", &guess);
        if (guess == secret_number) {
            printf("You won in %d guesses!\n\n", num_guesses);
            return;
        } else if (guess < secret_number) {
            printf("Too low; Try again.\n");
        } else
            printf("Too high; Try again.\n");
    }
}